import json
import re
import time

import requests
from DrissionPage import ChromiumOptions, Chromium
from datetime import datetime

# 最大下载数量
MAX_DOWNLOAD_COUNT = 1000
download_count = 0

headers = {
    'referer': 'https://v3-web.douyinvod.com/',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'
}

Page = ChromiumOptions()
browser = Chromium(Page)
tab = browser.latest_tab
tab.listen.start('https://www.douyin.com/aweme/v1/web/search/item/')
tab.get(
    'https://www.douyin.com/search/%E4%BC%8A%E5%88%A9%E6%99%AE%E9%80%9A%E8%AF%9D%E6%80%8E%E4%B9%88%E8%AF%B4?modal_id=7457564432898460987&type=video')

# 增加监听时间
time.sleep(5)

for packet in tab.listen.steps():
    if download_count >= MAX_DOWNLOAD_COUNT:
        break
    time.sleep(1)
    tab.scroll.to_bottom()
    time.sleep(2)
    B_data = packet.response.body

    # 增加内层循环范围
    for i in range(20):
        if download_count >= MAX_DOWNLOAD_COUNT:
            break
        try:
            download_count += 1
            json_titme = B_data['data'][i]['aweme_info']['desc']
            json_url = B_data['data'][i]['aweme_info']['video']['download_addr']['url_list'][0]
            print(json_titme, json_url)
            now = datetime.now()
            data_1 = {
                "province": "新疆维吾尔自治区",
                "city": "乌鲁木齐市",
                "website": "抖音",
                "persons": {"crawl_person": "张亮"},
                "times": {"crawl_time": str(now)},
                "file": {
                    "duration": "10",
                    "local_filename": f"f:\\D:\\抖音\\新疆\\xinjiang-origin2\\6540_250326_00000{download_count}_origin.wav"
                }
            }

            # 增加重试机制
            max_retries = 3
            for retry in range(max_retries):
                try:
                    req = requests.get(json_url, headers=headers).content
                    break
                except requests.RequestException as e:
                    if retry < max_retries - 1:
                        print(f"请求失败，重试第 {retry + 1} 次: {e}")
                        time.sleep(2)
                    else:
                        print(f"请求失败，已达到最大重试次数: {e}")
                        continue

            time.sleep(2)
            with open(
                    f"D:\\Users\\Hasee\\Desktop\\工作\\抖音\\新疆\\xinjiang-origin2\\6540_250326_00000{download_count}_origin.mp4",
                    mode='wb') as f:
                f.write(req)
            print(f'下载完成{download_count}')
            json_data = json.dumps(data_1, ensure_ascii=False)
            with open(
                    r'D:\Users\Hasee\Desktop\工作\抖音\新疆\xinjiang-origin2\数据记录.txt',
                    'a', encoding='utf-8') as f:
                f.write(json_data)
                f.write('\n')
            print(json_data)
        except (KeyError, IndexError) as e:
            print(f"解析数据时出现错误: {e}")
        except Exception as e:
            print(f"处理视频时出现错误: {e}")
